﻿using System;
using System.Collections.Generic;
using jQueryApi;
using System.Diagnostics;
using System.Html;

namespace TuringMachine
{
   class Shop : MessageBoxBase
   {
      jQueryObject m_welcomeText = null;
      Label m_CreditsText;
      Label m_CreditsAmount;
      Div col1;
      Div col2;
      List<Div> m_lUpgradeIcons;
      Label m_toolTip;
      public int m_iCredits;

      public Shop(jQueryObject parent, int iCredits, List<Upgrade> allUpgrades, Upgrades dicBought) :
         base(parent)
      {
         m_iCredits = iCredits;
         string id = m_msgBox.GetAttribute("id");
         m_msgBox.CSS("width", "600px");
         m_msgBox.Prepend("<table style='margin-left:30px'><tr><td id='col1'></td><td id='col2'></td></tr></table>");
         m_msgBox.Prepend("<span id=\"" + id + "msg\"></span><br />");
         m_welcomeText = m_msgBox.Children("#" + id + "msg");
         m_welcomeText.CSS("width", (m_msgBox.GetWidth()) + "px");
         m_welcomeText.CSS("height", "100px");
         m_welcomeText.CSS("top", "10px");
         m_welcomeText.CSS("padding", "0px 10px 0px 10px");
         m_welcomeText.CSS("display", "table-cell");
         m_welcomeText.CSS("vertical-align", "middle");
         m_welcomeText.Html("<b>Welcome to the shop</b><br/><br/>You can buy and sell here upgrades. Upgrades can ease or even enable " +
            "some levels, so choose smartly!. Note that although you can sell upgrades as often as you want, you won't get the " +
            "original amount of credits back, just 75% of the buy price<br/>Hold the cursor over an item to get a description and click it to buy " +
            "or sell it. The transaction is performed when you press the 'ok' button, you can use the 'cancel' button to leave the shop " +
            "and undo all operations done");
         jQueryObject leftCell = m_msgBox.Find("#col1");
         jQueryObject rightCell = m_msgBox.Find("#col2");
         leftCell.Prepend("These are the items you can buy:");
         rightCell.Prepend("These are the items you can sell:");
         rightCell.CSS("padding-left", "30px");
         col1 = new Div(leftCell, "col1");
         col2 = new Div(rightCell, "col2");
         col1.pObject.CSS("width", ((m_msgBox.GetWidth() / 2) - 50) + "px");
         col1.pObject.CSS("height", "200px");
         col1.pObject.CSS("position", "static");
         col1.pObject.CSS("background-color", "#ffc0c0");
         col1.pObject.CSS("border", "2px solid #ff8080");
         col2.pObject.CSS("width", ((m_msgBox.GetWidth() / 2) - 50) + "px");
         col2.pObject.CSS("height", "200px");
         col2.pObject.CSS("position", "static");
         col2.pObject.CSS("background-color", "#c0ffc0");
         col2.pObject.CSS("border", "2px solid #60ff60");
         m_lUpgradeIcons = new List<Div>();
         int iOrd = 0;
         foreach (Upgrade up in allUpgrades)
         {
            Div parentCol;
            bool bBuy = !dicBought.HasUpgrade(up.m_sKey);
            if (bBuy)
               parentCol = col1;
            else
               parentCol = col2;
            Div icon = new Div(parentCol.pObject, "icon");
            m_lUpgradeIcons.Add(icon);
            icon.pObject.AddClass("cell");
            icon.pObject.AddClass("upg" + iOrd);
            icon.pObject.CSS("width", "32px");
            icon.pObject.CSS("height", "32px");
            icon.pObject.CSS("position", "static");
            icon.pObject.CSS("margin", "5px 5px 5px 5px");
            icon.pObject.CSS("display", "inline-block");
            icon.pObject.Data("ord", iOrd);
            icon.pObject.Data("upgrade", up);
            icon.pObject.Data("buy", bBuy);
            icon.pObject.MouseEnter(delegate(jQueryEvent ev)
            {
               m_toolTip.pVisible = true;
               jQueryObject target = jQuery.FromElement(ev.CurrentTarget);
               Upgrade upg = ((Upgrade)target.GetDataValue("upgrade"));
               bool bBought = target.Parent().GetAttribute("id") != col1.pObject.GetAttribute("id");
               int credits = upg.m_iCredits;
               string color;
               string msg;
               if (bBought)
               {
                  credits = Math.Truncate(credits * 0.75);
                  color = "Green";
                  msg = "Click to sell<br /> for " + credits + " credits";
               }
               else if (credits <= m_iCredits)
               {
                  color = "Red";
                  msg = "Click to buy<br /> for " + credits + " credits";
               }
               else
               {
                  color = "Grey";
                  msg = "You'll need " + credits + " credits <br/>to buy this upgrade";
               }
               m_toolTip.pHtml = "<b><u><p style='float:left;margin-top:3px;'>" + upg.m_sName +
                  "</p></u><p style='color:" + color + ";float:right;margin-top:0px'>" + msg + "</p></b><p style='clear:both;'>" +
                  upg.m_sDescription +
                  "</p>";
            });
            icon.pObject.MouseLeave(delegate(jQueryEvent ev)
            {
               m_toolTip.pVisible = false;
            });
            icon.pObject.MouseMove(delegate(jQueryEvent ev)
            {
               if (m_toolTip.pVisible)
               {
                  m_toolTip.pPosX = GetOffsetX(ev) + 10;
                  m_toolTip.pPosY = GetOffsetY(ev) + 20;
               }
            });
            icon.pObject.Click(delegate(jQueryEvent ev)
            {
               jQueryObject target = jQuery.FromElement(ev.CurrentTarget);
               Upgrade upg = ((Upgrade)target.GetDataValue("upgrade"));
               bool bBought = target.Parent().GetAttribute("id") != col1.pObject.GetAttribute("id");
               if (bBought)
               {
                  m_toolTip.pVisible = false;
                  target.Detach();
                  target.AppendTo(col1.pObject);
                  m_iCredits = int.Parse(m_iCredits.ToString()) + Math.Truncate(upg.m_iCredits * 0.75);
               }
               else if (m_iCredits >= upg.m_iCredits)
               {
                  m_toolTip.pVisible = false;
                  target.Detach();
                  target.AppendTo(col2.pObject);
                  m_iCredits -= upg.m_iCredits;
               }
               m_CreditsAmount.pText = m_iCredits.ToString();
            });
            iOrd++;
         }

         m_toolTip = new Label(parent, "tooltip");

         m_toolTip.pObject.CSS("position", "absolute");
         m_toolTip.pVisible = false;
         m_toolTip.pObject.CSS("width", "300px");
         m_toolTip.pObject.CSS("position", "relative");
         m_toolTip.pObject.CSS("overflow", "auto");
         m_toolTip.pObject.CSS("padding", "5px");
         m_toolTip.pObject.CSS("background-color", "#ffff00");
         m_toolTip.pObject.CSS("border", "1px solid black");
         m_toolTip.pObject.CSS("z-index", "210");

         m_CreditsText = new Label(m_msgBox, "creditsText");
         m_CreditsText.pObject.CSS("position", "static");
         m_CreditsAmount = new Label(m_msgBox, "creditsAmount");
         m_CreditsAmount.pObject.CSS("position", "static");
         m_CreditsText.pText = "Your current credits :";
         m_CreditsAmount.pText = m_iCredits.ToString();
      }


      private int GetOffsetX(jQueryEvent ev)
      {
         return ev.PageX - m_PosX;
      }

      private int GetOffsetY(jQueryEvent ev)
      {
         return ev.PageY - m_PosY;
      }

      protected override void PlaceElements()
      {
         m_msgBox.CSS("height", "500px");
      }

      protected override void OkButton_Click(TMButton sender)
      {
         Upgrades upgList = new Upgrades("");
         foreach (Div icon in m_lUpgradeIcons)
         {
            bool bBought = icon.pObject.Parent().GetAttribute("id") != col1.pObject.GetAttribute("id");
            Upgrade upg = ((Upgrade)icon.pObject.GetDataValue("upgrade"));
            if (bBought)
               upgList.SetUpgrade(upg.m_sKey, true);
         }
         Window.LocalStorage.SetItem("TM_UpgradesBought", upgList.GetUpgradeString());
         Window.LocalStorage.SetItem("TM_Credits", m_iCredits);
         base.OkButton_Click(sender);
      }
   }
}

